package dev.alm.alertagentservice.tool;

import dev.alm.alertagentservice.bean.Alert;
import dev.alm.alertagentservice.bean.DictTypeParam;
import dev.alm.alertagentservice.bean.UpdateDictTypeStatusParam;
import dev.alm.alertagentservice.service.AlertService;
import dev.alm.alertagentservice.service.ModelService;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import io.micrometer.common.util.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class AlertTool {

    @Resource
    private AlertService alertService;

    @Resource
    private ModelService modelService;

    @Tool(name = "查询系统编码配置", value = "查询系统中的系统编码配置")
    public List<DictTypeParam> getDictTypeList() {
        List<DictTypeParam> dictTypeList = modelService.getDictTypeList();
        return dictTypeList;
    }

    @Tool(name = "启用类型编码", value = "启用类型编码")
    public String startDictType(@P("编号列表") List<Long> ids) {
        UpdateDictTypeStatusParam updateDictTypeStatusParam = new UpdateDictTypeStatusParam();
        updateDictTypeStatusParam.setIds(ids);
        updateDictTypeStatusParam.setDictStatus("1");
        String message = modelService.startDictType(updateDictTypeStatusParam);
        return message;
    }

    @Tool(name = "停用类型编码", value = "启用类型编码")
    public String stopDictType(@P("编号列表") List<Long> ids) {
        UpdateDictTypeStatusParam updateDictTypeStatusParam = new UpdateDictTypeStatusParam();
        updateDictTypeStatusParam.setIds(ids);
        updateDictTypeStatusParam.setDictStatus("0");
        String message = modelService.startDictType(updateDictTypeStatusParam);
        return message;
    }

    @Tool(name = "删除系统编码", value = "删除系统编码")
    public String removeDictType(@P("类型编码") List<Long> ids) {
        DictTypeParam dictTypeParam = new DictTypeParam();
        dictTypeParam.setIds(ids);
        String message = modelService.removeDictType(dictTypeParam);
        return message;
    }

//    @Tool(name = "更新系统编码，修改名称，修改编码，启用，停用等", value = "更新系统编码，修改名称，修改编码，启用，停用等")
//    public String updateDictType(@P("编号") Long id, @P("类型编码") String dictCode, @P("类型名称") String dictName, @P("状态") String status) {
//        // 先查询出这条记录
//        List<DictTypeParam> dictTypeList = modelService.getDictTypeList();
//        DictTypeParam dictTypeParam = new DictTypeParam();
//        for (DictTypeParam typeParam : dictTypeList) {
//            if (typeParam.getDictCode().equals(dictCode)) {
//                dictTypeParam = typeParam;
//                break;
//            }
//        }
//        if (!StringUtils.isEmpty(status)) {
//            Integer statusCode;
//            if (status == "启用") {
//                statusCode = 1;
//            } else {
//                statusCode = 0;
//            }
//            dictTypeParam.setDictStatus(statusCode);
//        }
//        if (!StringUtils.isEmpty(dictCode)) {
//            dictTypeParam.setDictCode(dictCode);
//        }
//        if (!StringUtils.isEmpty(dictName)) {
//            dictTypeParam.setDictName(dictName);
//        }
//        String s = modelService.updateDictType(dictTypeParam);
//        return s;
//    }

    @Tool(name = "创建系统编码", value = "创建系统编码")
    public String addDictType(@P("类型编码") String dictCode, @P("类型名称") String dictName, @P("状态") String status) {
        Integer statusCode;
        if (status == "启用") {
            statusCode = 1;
        } else {
            statusCode = 0;
        }
        DictTypeParam dictTypeParam = new DictTypeParam();
        dictTypeParam.setDictCode(dictCode);
        dictTypeParam.setDictName(dictName);
        dictTypeParam.setDictStatus(statusCode);
        String message = modelService.addDictType(dictTypeParam);
        return message;
    }

    @Tool(name = "工单数量查询", value = "查询当前用户名下的工单数量")
    public Long getAlertCount(@P("姓名") String handler) {
        return alertService.getAlertCountByHandler(handler);
    }

    @Tool(name = "工单列表查询", value = "查询当前用户名下的工单列表")
    public List<Alert> getAlertList(@P("姓名") String handler) {
        return alertService.getAlertList(handler);
    }

    @Tool(name = "工单流转", value = "根据用户提供的工单id更新工单状态，必须提供新的工单状态（alert_status）以及流转原因（reason），如果用户没有提供这些必要的信息，则返回给用户，提示他（她）必须提供，不要为用户生成工单状态或流转原因")
    public String updateAlert(@P("工单id") Long id, @P("工单状态") String alertStatus, @P("流转原因") String reason) {
        if (id == null) {
            return "请提供工单id";
        }
        // 先查询工单是否存在
        Alert alert = alertService.getAlertById(id);
        if (alert == null) {
            return "工单不存在";
        }
        if (alertStatus == null) {
            return "请提供工单状态";
        }
        if (reason == null) {
            return "请提供流转原因";
        }
        alert.setAlertStatus(alertStatus);
        alert.setReason(reason);
        Boolean b = alertService.updateAlert(alert);
        if (!b) {
            return "工单流转失败";
        }
        return "工单流转成功";
    }


}
